package com.woniuxy.weixindemo.util;

import com.sun.istack.internal.NotNull;
import com.woniuxy.weixindemo.enums.ResultCodeEnum;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.HashMap;

/**
 * 全局统一返回结果类
 *
 * @author admin
 */
@Data
@EqualsAndHashCode(callSuper = true)
@SuppressWarnings("all")
public class Result extends HashMap<String, Object> {

    public Result() {
    }

    protected static Result build() {
        Result result = new Result();
        return result.date(null);
    }

    protected static Result build(Object data) {
        Result result = new Result();
        result.date(data);
        return result;
    }


    public static Result build(Object data, @NotNull ResultCodeEnum resultCodeEnum) {
        Result result = build(data);
        result.code(resultCodeEnum.getCode());
        result.message(resultCodeEnum.getMessage());
        return result;
    }

    public static Result build(@NotNull ResultCodeEnum resultCodeEnum) {
        Result result = build();
        result.code(resultCodeEnum.getCode());
        result.message(resultCodeEnum.getMessage());
        return result;
    }


    public static Result build(Integer code, String message) {
        Result result = build();
        result.code(code);
        result.message(message);
        return result;
    }

    public static Result build(Object data, Integer code, String message) {
        Result result = build(data);
        result.code(code);
        result.message(message);
        return result;
    }

    public static Result ok() {
        return Result.ok(null);
    }

    /**
     * 操作成功
     *
     * @param data data数据
     * @return 返回Result
     */
    public static Result ok(Object data) {
        Result result = build(data);
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static Result fail() {
        return Result.fail(null);
    }

    /**
     * 操作失败
     *
     * @param data data数据
     * @return 返回Result
     */
    public static Result fail(Object data) {
        Result result = build(data);
        return build(data, ResultCodeEnum.FAIL);
    }

    public boolean isOk() {
        String key = "code";
        return (Integer) super.get(key) == ResultCodeEnum.SUCCESS.getCode().intValue();
    }


    public Result date(Object data) {
        super.put("data", data);
        return this;
    }

    public Result code(Integer code) {
        super.put("code", code);
        return this;
    }

    public Result message(String message) {
        super.put("message", message);
        return this;
    }

    public Result otherData(String key, Object val) {
        super.put(key, val);
        return this;
    }

    public Integer getCode() {
        return (Integer) super.get("key");
    }


    public String getMessage() {
        return super.get("key").toString();
    }

    public Object getData() {
        return super.get("data");
    }


}
